package com.zhiyou100.blog.aciton.Endorse;

import com.alibaba.fastjson.JSON;
import com.zhiyou100.blog.service.EndorseService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author shkstart
 * @create 2021-10-26 11:16
 */
@WebServlet("/endorse/updatezx")
public class EndorseUpdateServlet extends HttpServlet {
    private EndorseService service = new EndorseService();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String mid = request.getParameter("mid");
        String uid = request.getParameter("uid");
        String tid = request.getParameter("tid");
        String text = request.getParameter("text");
        int t = Integer.parseInt(tid);
        int m = Integer.parseInt(mid);
        int u = Integer.parseInt(uid);

        System.out.println("text = " + text);
        String message = "";
        //修改赞的内容, 如果 等于 0说明没有点过赞
        if ("赞".equals(text)) {
            int i = service.updateEndorseZanXu(m, u, true);
            if (i == 0) {
                service.addEndorseZanXu(m, u, true);
                message = "点赞成功";
            } else {
                message = "修改成功";
            }
        } else { //修改虚的内容
            int i = service.updateEndorseZanXu(m, u, false);
            System.out.println(i);
            if (i == 0) {
                service.addEndorseZanXu(m, u, false);
                message = "点虚成功";
            } else {
                message = "修改成功";
            }
        }


        response.getWriter().print(JSON.toJSONString(message));
        //跳转页面重新刷新数据
        request.getRequestDispatcher("/title/showMessage?tid=" + t).forward(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
